package com.xiegk.ticket;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author xgk
 * @date 2022/2/20
 */
@Component
public class TicketRedissonLock {
    private int number = 50;
    @Autowired
    private RedissonClient redissonClient;

    // 分布式锁: 版本3 使用 Redisson
    public void sale() {
        RLock lock = redissonClient.getLock("ticket:lock");
        lock.lock();
        try {
            if (number > 0) {
                System.out.println(Thread.currentThread().getName() + "\t" + "卖出第" + (number--) + "\t还剩下：" + number);
            }
        } finally {
            lock.unlock();
        }
    }
}
